/*
 * Time : 2023/3/11 14:00
 * Auth : weixinlong
 * Email : weixinlong@buaa.edu.cn
 * File : mission.go
 */

package model

import (
	"fmt"
	"gorm.io/gorm"
)

func AddMission(db *gorm.DB, value *Mission) error {
	if value.Name == "" {
		return fmt.Errorf("missing mission_name")
	}

	return db.Create(value).Error
}

func DeleteMission(db *gorm.DB, id uint, isAdmin bool, authorId uint) error {
	var origin Mission
	origin.ID = id
	if err := db.Take(&origin).Error; err != nil {
		return err
	}
	if !isAdmin && origin.AuthorID != authorId {
		return fmt.Errorf("normal user can only modify your own label")
	}
	return db.Delete(&Mission{}, id).Error
}

func FindMissionByID(db *gorm.DB, id uint) (*Mission, error) {
	ret := Mission{}
	ret.ID = id
	if err := db.Take(&ret).Error; err != nil {
		return nil, err
	}
	return &ret, nil
}
